
    <div class="content margin_top">
        <header class="header fixed_top bg_mx_green"></header>
        <form class="farmerinfo"></form>
    </div>
    <div class="toast"></div>
    <!-- toast模版 -->
    <script type="text/template" charset="utf-8" id='toast_template'>
        <i class="iconfont icon-toast{{=it.icon}}"></i>
        <div class="toast_text">{{=it.text}}</div>
    </script>
    <script type="text/template" charset="utf-8" id='farmer_template'>
        <div class="bg_white margin_b_10">
            <div class="flex border_b padding_10_15 border_b">
                <div class="width_70">基本情况</div>
            </div>
            <div class="padding_h_15 padding_v_5 border_b row lineheight_3">
                <div class="col_6 flex">
                    <div class="width_70">名称：</div>
                    <div class="left_70">
                        <input type="text" name="name" value="{{=it.name||''}}" placeholder="农家乐名称" request title="农家乐名称">
                    </div>
                </div>
                <div class="col_6 flex">
                    <div class="width_70">房间数：</div>
                    <div class="left_70">
                        <input type="text" name="number" value="{{=it.roomnum||''}}" placeholder="房间数" request title="房间数" data-pattern="^\d*$" data-tips="整数">
                    </div>
                </div>
                <div class="col_6 flex">
                    <div class="width_70">可吃饭：</div>
                    <div class="left_70">
                        <select name="canfeed">
                            <option value="1" {{if(it.canfeed){}}selected{{}}}>是</option>
                            <option value="0" {{if(!it.canfeed){}}selected{{}}}>否</option>
                        </select>
                    </div>
                </div>
                <div class="col_6 flex">
                    <div class="width_100">可接待人数：</div>
                    <div class="left_100">
                        <input type="text" name="max_num" value="{{=it.caninnum||''}}"  placeholder="人数" request title="可接待人数" data-pattern="^\d*$" data-tips="整数">
                    </div>
                </div>
                <div class="col_6 flex">
                    <div class="width_70">价格：</div>
                    <div class="left_120">
                        <input type="text" name="price" placeholder="价格"  value="{{=it.price||''}}"  title="价格" request data-pattern="^[0-9]+(.[0-9]{1,2})?$" data-tips="整数或最多两位小数">
                    </div>
                    <div class="width_50">/人/天</div>
                </div>
                <div class="col_12 flex">
                    <div class="width_70">环境介绍</div>
                    <div class="left_70">
                        <textarea class="padding_v_5" name="envdesc" placeholder="环境介绍" rows="5">{{=it.envdesc||''}}</textarea>
                    </div>
                </div>
                <div class="col_12 flex">
                    <div class="width_70">食宿</div>
                    <div class="left_70">
                        <textarea class="padding_v_5" name="accommodation" placeholder="食宿" rows="5">{{=it.accommodation||''}}</textarea>
                    </div>
                </div>
            </div>
        </div>
        <div class="flex border_b padding_10_15 border_b bg_white">
            <div class="width_70">图片</div>
        </div>
        <div class="padding_15 bg_white margin_b_10">
            <div class="preview0 flex_row_3 margin_b_10"></div>
            <div class="light lineheight_3 v_center input0">
                <div class="width_50 border text_center relative">
                    <div class="iconfont icon-add font_20"></div>
                    <input class="absolute all" type="file" value="" accept="image/*" style="opacity:0;" onchange="selectpic(this,0)"/>
                    <input type="hidden" name="envphoto" value=""/>
                </div>
                <div class="left_50  padding_l_15">点击上传环境图片</div>
            </div>
        </div>
        <div class="padding_15 bg_white margin_b_10">
            <div class="preview1 flex_row_3 margin_b_10"></div>
            <div class="light lineheight_3 v_center input1">
                <div class="width_50 border text_center relative">
                    <div class="iconfont icon-add font_20"></div>
                    <input class="absolute all" type="file" value="" accept="image/*" style="opacity:0;" onchange="selectpic(this,1)"/>
                    <input type="hidden" name="accommodationphoto" value=""/>
                </div>
                <div class="left_50  padding_l_15">点击上传食宿图片</div>
            </div>
        </div>
        <div class="padding_15 font_16">
            <button class="btn btn_mx_green all_width lineheight_3">保存</button>
        </div>
        <input name="_csrf" type="hidden" id="_csrf" value="<{$app->request->csrfToken}>">
        <input name="farmerid" type="hidden" id="farmerid" value="<{$app->request->get('farmerid')}>">
    </script>
    <script type="text/template" charset="utf-8" id='img_template'></script>
    <script type="text/template" charset="utf-8" id='img_one_template'>
        <div class="row_one relative">
            <div class="del_img" onclick="del({{=it.n}},{{=it.i}})">&times;</div>
            <img class="fit_img2" src="{{=it.d}}">
        </div>
    </script>
    <script>
        // 页面初始化
            var house = <{$jsonhouse}>||{},img_old=[house.envphoto||[],house.accommodationphoto||[]],img_arr=[[],[]],imgs=[[],[]],evalImg,evalimg_one = doT.template($("#img_one_template").text());console.log(house);
            var evalfarmer = doT.template($("#farmer_template").text());
            var evalToast = doT.template($("#toast_template").text());
            loaddata(".farmerinfo",evalfarmer,house);
            loadheadbar("申请农家乐",1);
            loadimg(function(){
                evalImg = doT.template($("#img_template").text());
                for (var i = 0; i < img_old.length; i++) {
                    $(".preview"+i).html(evalImg({n:i,d:img_old[i],already:1}))
                    if($(".preview"+i).find(".row_one").length >= 3){
                        $(".input"+i).hide();
                    }
                }
            })
        // 选择图片
            function selectpic(obj,i){
                var file = obj.files[0];
                var URL = window.URL || window.webkitURL;
                var preview = URL.createObjectURL(file);
                tobase64(file,function(res){
                    imgs[i].push(res);
                })
                img_arr[i].push(preview);
                $(obj).val("");
                $(".preview"+i).append(evalimg_one({n:i,i:img_arr[i].length-1,d:preview}))
                if($(".preview"+i).find(".row_one").length >= 3){
                    $(".input"+i).hide();
                }
            }
        // 删除图片
            function del(n,i,already){
                $(".input"+n).show();
                if(already){
                    var type = (n==0)?"envphoto":"accommodationphoto";
                    $.post("/business/person/delete-house-img",{type:type,id:house.id,key:i,_csrf:'<{$app->request->csrfToken}>'},function(res){
                        toast("attention",res.msg)
                        if(!res.status){
                            img_old[n].splice(i,1);
                            init_img(n)
                        }
                    },"json")
                }else{
                    imgs[n].splice(i,1);
                    img_arr[n].splice(i,1);
                    init_img(n)
                }
            }
            function init_img(n){
                $(".preview"+n).html("")
                if (img_old[n]) {
                    $(".preview"+n).html(evalImg({n:n,d:img_old[n],already:1}))
                }
                if (img_arr[n]) {
                    $(".preview"+n).append(evalImg({n:n,d:img_arr[n]}))
                }
            }
        // 提交表单
            var submited = false;
            $(".farmerinfo").submit(function(e){
                console.log(img_arr)
                e.preventDefault();
                with(this){
                    envphoto.value = imgs[0].join("%^");
                    accommodationphoto.value = imgs[1].join("%^");
                    for (var i = 0; i < this.length; i++) {
                        if($(this[i]).attr("request")==""){
                            if(!checkone(this[i])){
                                return false;
                            }
                        }
                    }
                    if(!$(".preview0").find(".row_one").length){
                        toast("attention","请上传环境图片");
                        return false;
                    }
                    if(!$(".preview1").find(".row_one").length){
                        toast("attention","请上传食宿图片");
                        return false;
                    }
                    if(submited) return false;
                    var data = $(".farmerinfo").serialize();
                    $.post("/business/person/post-farmer-request",data,function(res){
                        toast("attention",res.msg);
                        submited = true;
                        if(res.status == 0){
                             location.href='/business/person/farmer';
                        }
                    },"json");
                }
            })
    </script>


